------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--[[


    根据表格批量配置 netvars

        net_bool                1-bit boolean
        net_tinybyte            3-bit unsigned integer   [0..7]
        net_smallbyte           6-bit unsigned integer   [0..63]
        net_byte                8-bit unsigned integer   [0..255]
        net_shortint            16-bit signed integer    [-32767..32767]
        net_ushortint           16-bit unsigned integer  [0..65535]
        net_int                 32-bit signed integer    [-2147483647..2147483647]
        net_uint                32-bit unsigned integer  [0..4294967295]
        net_float               32-bit float
        net_hash                32-bit hash of the string assigned
        net_string              variable length string
        net_entity              entity instance
        net_bytearray           array of 8-bit unsigned integers (max size = 31)
        net_smallbytearray      array of 6-bit unsigned integers (max size = 31)
        net_ushortarray         array of 16-bit unsigned integers (max size = 31)

    local spells_data = {
                    ["test_spell"] = { 
                                        type = net_bool ,  -- net_bool 
                                        namespace = nil,        --- 缺省的时候自动为spell_index
                                        event = "test_spell"
                                    },
    }

]]--
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
---
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

local spells_data = {
    ["test_spell"] = { fn = net_float , namespace = nil, event = "test_spell" },
    ["energy_saving_mode"] = { fn = net_bool , namespace = nil, event = "loramia_event.energy_saving_mode"},
    ["creation_impact"] = { fn = net_float , namespace = nil, event = nil},
}


------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
---
    local function Create_Net_Vars(inst)
        for spell_index,spell_data in pairs(spells_data) do
            local fn = spell_data.fn
            local namespace = spell_data.namespace or spell_index
            local event = spell_data.event or nil            
            inst.netvars[spell_index] = fn(inst.GUID,namespace,event)
        end
    end
    local function CreateEvents(inst,player)
        for spell_index,spell_data in pairs(spells_data) do
            local fn = spell_data.fn
            local namespace = spell_data.namespace or spell_index
            local event = spell_data.event or nil
            if event then
                inst:ListenForEvent(event,function()
                    player:PushEvent(event)
                end)
            end
        end
    end
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
---
    local function LinkPlayer(inst,player,com)
        inst.entity:SetParent(player.entity)
        inst.Network:SetClassifiedTarget(player)
        if TheWorld.ismastersim then
            if com and com.AttachClassified then
                com:AttachClassified(inst)
            else
                inst:DoTaskInTime(0,function()
                    player.components.loramia_com_spell_controller:AttachClassified(inst)                    
                end)
            end
        end
        local replica_com = player.replica.loramia_com_spell_controller or player.replica._.loramia_com_spell_controller
        if replica_com then
            replica_com:AttachClassified(inst)
            if not TheNet:IsDedicated() then
                CreateEvents(inst,player)
            end
            print("info loramia_other_spells_classified AttachClassified succeed")
            return
        end
        print("error loramia_other_spells_classified AttachClassified failed")
    end
    local function OnEntityReplicated(inst)
        local player = inst.entity:GetParent()
        if player then
            LinkPlayer(inst,player)
        end        
    end
    local function Get(inst,index)
        if inst.netvars[index] then
            return inst.netvars[index]:value()
        end
    end
    local function Set(inst,index,value)
        if inst.netvars[index] then
            inst.netvars[index]:set(value)
        end
    end
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
local function fn()
    local inst = CreateEntity()

    inst.entity:AddTransform() --So we can follow parent's sleep state
    inst.entity:AddMapExplorer()
    inst.entity:AddNetwork()
    inst.entity:Hide()
    inst:AddTag("CLASSIFIED")

    inst.entity:SetPristine()
    inst.LinkPlayer = LinkPlayer
    inst.netvars = {}
    Create_Net_Vars(inst)
    inst.OnEntityReplicated = OnEntityReplicated
    inst:DoTaskInTime(0,OnEntityReplicated)
    inst.Get = Get
    inst.Set = Set

    inst.__linked_player = net_entity(inst.GUID,"loramia_other_spells_classified","linked_player")
    inst:ListenForEvent("linked_player",function()
        local player = inst.__linked_player:value()
        inst:LinkPlayer(player)
    end)

    if not TheWorld.ismastersim then
        return inst
    end

    return inst
end

return Prefab("loramia_other_spells_classified", fn)